class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ret;
        int n = intervals.size();
        sort(intervals.begin(), intervals.end());
        for(int i = 0; i < n; ++i)
        {
            int l = intervals[i][0], r = intervals[i][1];
            if(ret.size() == 0 || ret.back()[1] < l) ret.emplace_back({ l, r });
            else
                ret.back()[1] = max(ret.back()[1], r);
        }
        return ret;
    }
};
